<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Activity extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Activitymodel','m_activity',true);
        $this->load->model('Templatemodel','m_template',true);
    }

    public function index(){
        $data = parent::index();

        $data['attr'] = array();
        $data['attr']['template_list'] = $this->m_template->getActiveTemplateList();

        $this->load->view('admin/activity',$data);
    }

    public function getActivityList(){
        $assist['where'] = getFilter($this->input->post(),Activitymodel::$searchField);
        $assist['like'] = getFilter($this->input->post(),Activitymodel::$likeField);
        $assist['sort'] = getSort($this->input->post(),Activitymodel::$sortField);
        $assist['pagination'] = getPagination($this->input->post());
        $assist['join_user'] = true;

        list($list,$count) = $this->m_activity->getActivityListSection($assist);

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function createActivity(){
        $this->validate->setRule('activity_name','string');
        $this->validate->setRule('activity_url','string');
        $this->validate->setRule('template_id','integer');
        if(!$this->validate->run($this->input->post())){
          $this->response->setDetail($this->validate->getMessage());
          $this->response->responseExit();
        }
        $info = $this->validate->getResult();


        $info['user_id'] = $this->m_app->getCurrentUserId();
        $info['activity_status'] = STATUS_PENDING;
        $info['activity_time_create'] = NOW;
        $info['activity_code'] = uniqid('act');
        if($this->m_activity->createActivity($info) == 0) {
          $this->response->setDetail('创建失败');
          $this->response->responseExit();
        }

        $this->response->setSuccess();
        $this->response->responseExit();
      }

    public function editActivity(){
  		$this->validate->setRule('activity_id','integer');
      $this->validate->setRule('activity_name','string');
      $this->validate->setRule('activity_url','string');
      $this->validate->setRule('template_id','integer');
  		if(!$this->validate->run($this->input->post())){
  			$this->response->setDetail($this->validate->getMessage());
  			$this->response->responseExit();
  		}
  		$info = $this->validate->getResult();

  		$info['user_id'] = $this->m_app->getCurrentUserId();
  		$this->m_activity->editActivity($info);

  		$this->response->setSuccess();
  		$this->response->responseExit();
  	}

    public function deleteActivity(){
  		$this->validate->setRule('activity_id','integer');
      if(!$this->validate->run($this->input->post())){
        $this->response->setDetail($this->validate->getMessage());
        $this->response->responseExit();
      }
      $info = $this->validate->getResult();
      $info['user_id'] = $this->m_app->getCurrentUserId();
      $info['activity_status'] = STATUS_DELETE;

      $this->m_activity->editActivity($info);


      $this->response->setSuccess();
      $this->response->responseExit();
    }

    public function makeActivityUrl() {
      $this->validate->setRule('activity_id','integer');
      if(!$this->validate->run($this->input->post())){
        $this->response->setDetail($this->validate->getMessage());
        $this->response->responseExit();
      }
      $info = $this->validate->getResult();
      $activity = $this->m_activity->getActivity($info['activity_id']);

      $info['user_id'] = $this->m_app->getCurrentUserId();
      $info['activity_url_status'] = STATUS_PENDING;
      $info['activity_url_code'] = uniqid('hb');
      $info['activity_url_content'] = $activity['activity_url'].'?activity='.$activity['activity_code'].'&code='.$info['activity_url_code'];

      if($this->m_activity->createActivityUrl($info) == 0) {
        $this->response->setDetail('创建失败');
        $this->response->responseExit();
      }

      $this->response->setSuccess();
      $this->response->setAddon(array(
        'url' => $info['activity_url_content'],
      ));
      $this->response->responseExit();
    }


}
/* End of file api_log.php */
/* Location: ./application/controllers/api_log.php */
